package com.tanhua.server.service;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.model.ErrorResult;
import com.tanhua.model.User;
import com.tanhua.model.UserInfo;
import com.tanhua.model.Voice;
import com.tanhua.model.mongo.Video;
import com.tanhua.server.interceptor.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.utils.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@Service
@Slf4j
public class PeachblossomService {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @Autowired
    private FdfsWebServer webServer;

    @DubboReference
    private VideoApi videoApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private RedisTemplate redisTemplate;

    //桃花传音 发送语音
    public void peachblossom(MultipartFile soundFile) throws IOException {
        if (soundFile.isEmpty()) {
            throw new BusinessException(ErrorResult.error());
        }
        //1、将视频上传到FastDFS,获取访问URL
        String filename = soundFile.getOriginalFilename();  // test.wav
        filename = filename.substring(filename.lastIndexOf(".") + 1);
        StorePath storePath = fastFileStorageClient.uploadFile(soundFile.getInputStream(), soundFile.getSize(), filename, null);
        String voiceUrl = Constants.SOUNDS_RECOMMEND + UserHolder.getUserId() + webServer.getWebServerUrl();
        String voice = "voice";
        redisTemplate.opsForSet().add(voice, voiceUrl);
        System.out.println(voiceUrl);
    }

    //接受语音
    public Voice peachblossomGet() {
        //Key是voice
        String sound = "voice";
        //随机获取一个语音地址
        String voiceUrl = (String) redisTemplate.opsForSet().randomMember(sound);
        //切割两头字符串,获取key中间的登录Id
        String userId = StringUtils.substringBetween(voiceUrl, Constants.SOUNDS_RECOMMEND, webServer.getWebServerUrl());
        UserInfo userInfo = userInfoApi.findById(Long.valueOf(userId));
        Voice voice = Voice.init(userInfo);
        voice.setSoundUrl(voiceUrl.toString());
        Random random = new Random();
        voice.setId((int) (random.nextInt(2147483646)));
        Object remainingTimes = redisTemplate.opsForList().leftPop(Constants.SOUNDS_RemainingTimes + UserHolder.getUserId());
        voice.setRemainingTimes(Integer.parseInt(remainingTimes.toString()));
        return voice;
    }

    @DubboReference
    private UserApi userApi;

    @Scheduled(fixedDelay = 5000)
    public void setTimes() {
        log.info("--------定时任务开启--------");
        List<User> all = userApi.findAll();
        for (int i = 0; i < 10; i++) {
            redisTemplate.opsForList().leftPushAll(Constants.SOUNDS_RemainingTimes + UserHolder.getUserId().toString(), i);
        }


    }

}
